<template>
  <div class="app-container">
    <!-- 查询 -->
    <div class="filter-container">
      <el-row :gutter="0">
        <el-col :xs="24" :sm="6">
          <el-button class="filter-item" @click="refresh()">刷新</el-button>
        </el-col>
        <el-col :xs="24" :sm="18" style="text-align:right;">
          <el-button class="filter-item" @click="randKaohao()">随机生成考号</el-button>
          <el-button class="filter-item" @click="makeZuowei()">生成试场座位表</el-button>
          <el-button class="filter-item" @click="outExcel()">导出</el-button>
          <el-button class="filter-item" @click="addstudent()">批量添加学生</el-button>
          <el-button class="filter-item" @click="handleDelete()" type="danger" icon="el-icon-delete">批量删除</el-button>
          <el-button class="filter-item" type="primary" @click="add()">添加</el-button>
        </el-col>
      </el-row>
    </div>
    <!-- 列表 -->
    <el-table ref="multipleTable" v-loading="loading" :data="datas" :height="height" style="width: 100%" border @sort-change="sort" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="ID" min-width="30" sortable="custom"/>
      <el-table-column prop="student_xh" label="学号" min-width="50" sortable="custom"/>
      <el-table-column prop="student_name" label="学生" min-width="50" sortable="custom"/>
      <el-table-column prop="banji_name" label="班级" min-width="50" sortable="custom"/>
      <!-- <el-table-column prop="kaoshi_id" label="考试" min-width="100" sortable="custom"/> -->
      <el-table-column prop="xuhao" label="考号" min-width="40" sortable="custom"/>
      <el-table-column prop="zhunkaozhenghao" label="准考证号" min-width="50" sortable="custom"/>
      <el-table-column prop="shichangnum" label="试场号" min-width="50" sortable="custom"/>
      <el-table-column prop="zuoweihao" label="座位号" min-width="50" sortable="custom"/>
      <el-table-column prop="active" label="参与统计?" min-width="50" align="center" sortable="custom">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.active" :active-value="1" :inactive-value="0" disabled/>
        </template>
      </el-table-column>
      <el-table-column prop="zongfen" label="总分" min-width="50" sortable="custom"/>
      <el-table-column prop="mc_banji" label="班名次" min-width="50" sortable="custom"/>
      <el-table-column prop="mc_school" label="段名次" min-width="50" sortable="custom"/>
      <el-table-column label="操作" min-width="100" align="right" fixed="right">
        <template slot-scope="{ row }">
          <el-button size="mini" type="success" @click="edit(row)">修改</el-button>
          <el-button size="mini" type="danger" @click="del(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination v-show="count > 0" :total="count" :page.sync="query.page" :limit.sync="query.limit" @pagination="list" />
    <!-- 添加、修改 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialog" top="1vh" :before-close="cancel">
      <el-form ref="ref" :rules="rules" :model="model" label-width="100px" class="dialog-body" :style="{height:height+30+'px'}">
        <el-form-item label="学生" prop="student_id">
          <el-input v-model="model.student_id" placeholder="请输入学生" clearable />
        </el-form-item>
<!--        <el-form-item label="班级" prop="banji_id">
          <el-input v-model="model.banji_id" placeholder="请输入班级" clearable />
        </el-form-item>
        <el-form-item label="考试" prop="kaoshi_id">
          <el-input v-model="model.kaoshi_id" placeholder="请输入考试" clearable />
        </el-form-item>
        <el-form-item label="段名次" prop="mc2">
          <el-input v-model="model.mc2" placeholder="请输入段名次" clearable />
        </el-form-item>
        <el-form-item label="班名次" prop="mc1">
          <el-input v-model="model.mc1" placeholder="请输入班名次" clearable />
        </el-form-item>
        <el-form-item label="总分" prop="zongfen">
          <el-input v-model="model.zongfen" placeholder="请输入总分" clearable />
        </el-form-item> -->
        <el-form-item label="参与统计" prop="active">
          <el-switch v-model="model.active" :active-value="1" :inactive-value="0"/>
        </el-form-item>
        <el-form-item label="准考证号" prop="zhunkaozhenghao">
          <el-input v-model="model.zhunkaozhenghao" placeholder="请输入准考证号" clearable />
        </el-form-item>
        <el-form-item label="座位号" prop="zuoweihao">
          <el-input v-model="model.zuoweihao" placeholder="请输入座位号" clearable />
        </el-form-item>
        <el-form-item label="试场号" prop="shichangnum">
          <el-input v-model="model.shichangnum" placeholder="请输入试场号" clearable />
        </el-form-item>
        <el-form-item label="序号" prop="xuhao">
          <el-input v-model="model.xuhao" placeholder="请输入序号" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="submit">提交</el-button>
      </div>
    </el-dialog>
    <!-- 添加考生 -->
    <el-dialog :title="addDialogTitle" :visible.sync="addDialog" top="1vh" :before-close="addcancel">
      <div style="margin: 15px 0;"></div>
      <el-checkbox-group v-model="checkedBanjis">
        <el-checkbox v-for="banji in banjis" :label="banji.name" :key="banji.id">{{banji.name}}</el-checkbox>
      </el-checkbox-group>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addcancel">取消</el-button>
        <el-button type="primary" @click="batchAddStudent">添加</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import screenHeight from '@/utils/screen-height'
import Pagination from '@/components/Pagination'
import { list, info, add, edit, del,getUngraduatedBanjis,batchAddStudent,randKaohao,makeZuowei,multiDelete,outExcel } from '@/api/kaoshi_kaosheng'

export default {
  name: 'Kaoshi_kaosheng',
  components: { Pagination },
  props:['kaoshi_id','kaoshi_name'],
  data() {
    return {
      height: 680,
      loading: false,
      datas: [],
      count: 0,
      query: {
        page: 1,
        limit: 300
      },
      dialog: false,
      dialogTitle: '',
      addDialogTitle:'',
      addDialog:false,
      banjis:[],
      checkedBanjis:[],
      model: {
        id:'',
        student_id:'',
        banji_id:'',
        kaoshi_id:'',
        mc2:'',
        mc1:'',
        zongfen:'',
        active: 1,
        zhunkaozhenghao:'',
        zuoweihao:'',
        shichangnum:'',
        xuhao:'',
      },
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
      },
    }
  },
  created() {
    this.height = screenHeight()
    this.list()
    this.getUngraduatedBanjis()
  },
  methods: {
    outExcel(){
      outExcel({kaoshi_id:this.kaoshi_id}).then(res=>{
        this.downloadFile(res);
        console.log('ok');
      }).catch(function (error) {
        console.log(error);
      });
    },
    // 下载文件
    downloadFile (data) {
      if (!data) {
        return
      }
      let blob = new Blob([data])
      let fileName = this.kaoshi_name + '考生座位表.xls'
      if ('download' in document.createElement('a')) { // 不是IE浏览器
        let url = window.URL.createObjectURL(blob)
        let link = document.createElement('a')
        link.style.display = 'none'
        link.href = url
        link.setAttribute('download', fileName)
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link) // 下载完成移除元素
        window.URL.revokeObjectURL(url) // 释放掉blob对象
      } else { // IE 10+
        window.navigator.msSaveBlob(blob, fileName)
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleDelete() {
      let checkArr = this.multipleSelection;   // multipleSelection存储了勾选到的数据
      let params = [];
      let self = this;
      checkArr.forEach(function (item) {
        params.push(item.id);       // 添加所有需要删除数据的id到一个数组，post提交过去
      });
      this.loading = true
      multiDelete({ids:params}).then(res=>{
        this.$refs.multipleTable.clearSelection();
        this.list();
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    makeZuowei(){
      makeZuowei({kaoshi_id:this.kaoshi_id}).then(res=>{
        this.list()
        this.$message.success(res.msg)
      })
    },
    randKaohao(){
      randKaohao({kaoshi_id:this.kaoshi_id}).then(res=>{
        this.list()
        this.$message.success(res.msg)
      })
    },
    addcancel(){
      this.addDialog=false
    },
    addstudent(){
      this.addDialog=true
      this.addDialogTitle='批量添加学生'
    },
    batchAddStudent(){
      batchAddStudent({kaoshi_id:this.kaoshi_id,banjis:this.checkedBanjis}).then(res=>{
        this.checkedBanjis=[]
        this.list()
        this.addDialog=false
      })
    },
    getUngraduatedBanjis()
    {
      this.loading = true
      getUngraduatedBanjis().then(res => {
        this.banjis = res.data
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    // 列表
    list() {
      this.loading = true
      this.query.kaoshi_id = this.kaoshi_id
      list(this.query).then(res => {
        this.datas = res.data.list
        this.count = res.data.count
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    // 添加
    add() {
      this.dialog = true
      this.dialogTitle = '添加'
      this.reset()
    },
    edit(row) {
      this.dialog = true
      this.dialogTitle = '修改：' + row.id
      info({
        id: row.id
      }).then(res => {
        this.reset(res.data)
      })
    },
    // 删除
    del(row) {
      this.$confirm(
        '确定要删除 <span style="color:red">' + row.name + ' </span>吗？',
        '删除：' + row.id,
        { type: 'warning', dangerouslyUseHTMLString: true }
      ).then(() => {
        this.loading = true
        del({
          id: row.id
        }).then(res => {
          this.reset()
          this.list()
          this.$message.success(res.msg)
        }).catch(() => {
          this.loading = false
        })
      }).catch(() => {})
    },
    // 取消
    cancel() {
      this.dialog = false
      this.reset()
    },
    // 提交
    submit() {
      this.$refs['ref'].validate(valid => {
        if (valid) {
          this.loading = true
          if (this.model.id) {
            edit(this.model).then(res => {
              this.list()
              this.reset()
              this.dialog = false
              this.$message.success(res.msg)
            }).catch(() => {
              this.loading = false
            })
          } else {
            add(this.model).then(res => {
              this.list()
              this.reset()
              this.dialog = false
              this.$message.success(res.msg)
            }).catch(() => {
              this.loading = false
            })
          }
        }
      })
    },
    // 重置
    reset(row) {
      if (row) {
        this.model = row
      } else {
        this.model = this.$options.data().model
      }
      if (this.$refs['ref'] !== undefined) {
        this.$refs['ref'].resetFields()
      }
    },
    // 查询
    search() {
      this.query.page = 1
      this.list()
    },
    // 刷新
    refresh() {
      this.query = this.$options.data().query
      this.list()
    },
    sort(sort) {
      this.query.sort_field = sort.prop
      this.query.sort_type = ''
      if (sort.order === 'ascending') {
        this.query.sort_type = 'asc'
        this.list()
      }
      if (sort.order === 'descending') {
        this.query.sort_type = 'desc'
        this.list()
      }
    },
  }
}
</script>

<style scoped>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
</style>
